package com.sms.sendservice.aspect;

import com.alibaba.nacos.shaded.com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class GuavaManager implements ApplicationRunner {

    @Value("${qps}") // 读取配置文件中的qps值（如application.yml中的qps: 100）
    private Integer qps;

    // 非静态变量，由Spring管理单例
    private RateLimiter rateLimiter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        rateLimiter = RateLimiter.create(qps);
    }

    public boolean tryAcquire(){
        return rateLimiter.tryAcquire() ;
    }
}
